from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2 as cv


@index_bp.get('/spin_pro')
@index_bp.post('/spin_pro')
@view_logging_required
@permission_required("spin:pro")
def spin_pro():
    if request.method == "GET":
        return render_template('admin/cv/spin.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            file_url = file_read(f)
            pic = cv.imread(file_url)
            angle = request.form.get('angle')
            res_pic = "static/download/spin.jpg"
            rows, cols, channel = pic.shape
            # 绕图像的中心旋转
            # 参数：旋转中心 旋转度数 scale
            M = cv.getRotationMatrix2D((cols / 2, rows / 2), int(angle), 1)
            # 参数：原始图像 旋转参数 元素图像宽高
            out_image = cv.warpAffine(pic, M, (cols, rows))
            cv.imwrite(res_pic,out_image)
            return render_template('admin/cv/spin.html', pic=file_url, res_pic=res_pic)
        else:
            return "请传入正确的文件格式"
